package com.magenta.maxoptra.geocoding.geoinformsputnik;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

/**
 * User: Zlygostev
 * Date: 23.04.12
 * Time: 13:14
 */
public class InfoProvider {
    private static InfoProvider provider;
    private List<Project> projects;

    private InfoProvider(URL url) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Info.class);
            final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Info info = (Info) unmarshaller.unmarshal(url);
            projects = info.getProjects();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    private InfoProvider() {
        this(getGeoSamaraResourceUrl("/info.xml"));
    }

    public static URL getGeoSamaraResourceUrl(String resourceName) {
        return InfoProvider.class.getClassLoader().getResource("geosamara" + resourceName);
    }

    public static InputStream getGeoSamaraResourceStream(String resourceName) {
        return InfoProvider.class.getClassLoader().getResourceAsStream("geosamara" + resourceName);
    }

    public static synchronized InfoProvider getProvider() {
        if (provider == null) {
            provider = new InfoProvider();
        }
        return provider;
    }

    public Project getInfoForAddress(String address) {
        address = address.toUpperCase();
        for (Project project : projects) {
            if (address.contains(project.getName().toUpperCase())) {
                return project;
            }
        }
        return null;
    }

    public Project getSingleProject() {
        for (Project project : projects) {
            if ("SINGLE".equals(project.getName())) {
                return project;
            }
        }
        throw new IllegalStateException();
    }


    public List<Project> getProjects() {
        return projects;
    }

    //download resources
    public static void main(String[] args) {
        String url = "http://geoportal.samregion.ru";
        try {
            URL infoUrl = new URL(url + "/info");
            urlToFile(infoUrl, "c:/geosamara_resources/info.xml");
            InfoProvider infoProvider = new InfoProvider(infoUrl);
            for (Project project : infoProvider.getProjects()) {
                urlToFile(new URL(url + project.getSettingsUrl()), "c:/geosamara_resources" + project.getSettingsUrl() + ".xml");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void urlToFile(URL url, String fileName) throws IOException {
        url.openConnection();
        InputStream reader = url.openStream();
        FileOutputStream writer = new FileOutputStream(fileName);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = reader.read(buffer)) > 0) {
            writer.write(buffer, 0, bytesRead);
            buffer = new byte[1024];
        }
        writer.close();
        reader.close();
        System.out.println(url + " -> " + fileName);
    }
}
